//@Time:2022/4/2 21:42
//@Author:aFun

package indi.csms_springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import indi.csms_springboot.domain.*;
import indi.csms_springboot.mapper.CourseMapper;
import indi.csms_springboot.mapper.OfficerMapper;
import indi.csms_springboot.service.OfficerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Set;

@Service
public class OfficerServiceImpl implements OfficerService {

    @Autowired
    OfficerMapper officerMapper;

    @Autowired
    CourseMapper courseMapper;

    @Override
    public IPage<Course> findCourseJudgeByPage(String _currentPage, String _rows, Map<String, String[]> condition) {
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);
        IPage<Course> page=new Page<>(currentPage,rows);
        QueryWrapper<Course> wrapper = new QueryWrapper<>();

        Set<String> keySet = condition.keySet();
        for (String key : keySet) {
            if ("currentPage".equals(key) || "rows".equals(key)) {
                continue;
            }
            String value = condition.get(key)[0];
            if (value != null && !"".equals(value)) {
                wrapper.like(key,value);
            }
        }

        IPage<Course> courseIPage = courseMapper.selectPage(page, wrapper);
        return courseIPage;
    }

    @Override
    public boolean examineAndapprove(Course course) {
        boolean b = officerMapper.updateCourseJudge(course.getJudge(), course.getIsopen(), course.getCouId());
        return b;
    }

    @Override
    public IPage<CountResultVO> countResult(String _currentPage, String _row, Map<String, String[]> condition) {
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_row);
        Page<CountResultVO> page=new Page<>(currentPage,rows);
        QueryWrapper<CountResultVO> wrapper=new QueryWrapper<>();

        Set<String> keySet = condition.keySet();
        for (String key : keySet) {
            if ("currentPage".equals(key) || "rows".equals(key)) {
                continue;
            }
            String value = condition.get(key)[0];
            if (value != null && !"".equals(value)) {
                wrapper.like(key,value);
            }
        }

        IPage<CountResultVO> countResult = officerMapper.countResult(page, wrapper);

        return countResult;
    }

    @Override
    public IPage<StudentSelectCourseVO> selectionResult(String _currentPage, String _rows, Map<String, String[]> condition) {
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);
        Page<SSFOVO> page=new Page<>(currentPage,rows);
        QueryWrapper<SSFOVO> wrapper=new QueryWrapper<>();

        Set<String> keySet = condition.keySet();
        for (String key : keySet) {
            if ("currentPage".equals(key) || "rows".equals(key)) {
                continue;
            }
            String value = condition.get(key)[0];
            if (value != null && !"".equals(value)) {
                wrapper.like(key,value);
            }
        }

        IPage<StudentSelectCourseVO> studentSelectCourseVOIPage = officerMapper.selectionResult(page, wrapper);
        return studentSelectCourseVOIPage;
    }

    @Override
    public boolean deleteSelection(Integer stuId, Integer couId) {
        boolean b = officerMapper.deleteStu2Score(stuId, couId);
        return b;
    }

    @Override
    public IPage<SSFOVO> studentScore(String _currentPage, String _rows, Map<String, String[]> condition) {
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);
        Page<SSFOVO> page=new Page<>(currentPage,rows);
        QueryWrapper<SSFOVO> wrapper=new QueryWrapper<>();

        Set<String> keySet = condition.keySet();
        for (String key : keySet) {
            if ("currentPage".equals(key) || "rows".equals(key)) {
                continue;
            }
            String value = condition.get(key)[0];
            if (value != null && !"".equals(value)) {
                if(key.equals("stuId")){
                    key="ss.stuId";
                }
                wrapper.like(key,value);
            }
        }

        IPage<SSFOVO> studentScoreVOIPage = officerMapper.studentScore(page, wrapper);
        return studentScoreVOIPage;
    }
}
